package sg.edu.nus.comp.icreate.ivletalk.module;

import java.util.List;
import java.util.Map;

import sg.edu.nus.comp.icreate.ivletalk.ModuleNoRefreshBaseActivity;
import sg.edu.nus.comp.icreate.ivletalk.R;
import sg.edu.nus.comp.icreate.ivletalk.User;
import sg.edu.nus.comp.icreate.ivletalk.announcement.ListAnnouncementActivity;
import sg.edu.nus.comp.icreate.ivletalk.forum.ForumActivity;
import sg.edu.nus.comp.icreate.ivletalk.utils.net.DataPuller;
import sg.edu.nus.comp.icreate.ivletalk.voice.VoiceCommandManager;
import sg.edu.nus.comp.icreate.ivletalk.workbin.ListWorkbinElementActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ListModuleElementActivity extends ModuleNoRefreshBaseActivity {
	private Module module;
	private int modulePosition;
	private ListView elementList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_module_element);
		modulePosition = getIntent().getIntExtra(MainActivity.EXTRA_NAME_MODULE_POSITION, 0);
		module = MainActivity.modules.get(modulePosition);
		setTitle(module.getCode() + ": " + module.getName());
		elementList = (ListView) findViewById(R.id.elementList);
		elementList.setAdapter(new BaseAdapter() {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				LinearLayout view = new LinearLayout(getApplicationContext());
				view.setGravity(Gravity.CENTER_VERTICAL);
				ImageView icon = new ImageView(getApplicationContext());
				TextView text = new TextView(getApplicationContext());
				text.setPadding(ICON_TEXT_PADDING, 0, 0, 0);
				text.setTextColor(getResources().getColor(android.R.color.black));
				switch ((ElementType) getItem(position)) {
				case ANNOUNCEMENT:
					icon.setImageResource(R.drawable.info2);
					text.setText(ElementType.ANNOUNCEMENT.toString());
					break;
				case FORUM:
					icon.setImageResource(R.drawable.msg);
					text.setText(ElementType.FORUM.toString());
					break;
				case WORKBIN:
					icon.setImageResource(R.drawable.folder);
					text.setText(ElementType.WORKBIN.toString());
				}
				view.addView(icon);
				view.addView(text);
				return view;
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public Object getItem(int position) {
				return module.getExistence().get(position);
			}

			@Override
			public int getCount() {
				return module.getExistence().size();
			}
		});
		elementList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				((ListModuleElementActivity) parent.getContext()).goToItem(position);
			}
		});
		try {
			getElements();
		} catch (Exception e) {
			Log.e("err", "Error:Get Elements", e);
		}

		checkVoiceCommand();
	}

	private void getElements() throws Exception {
		module.getElements().clear();
		module.getExistence().clear();
		String url = "https://ivle.nus.edu.sg/api/Lapi.svc/Module?APIKey=" + MainActivity.APIKEY + "&AuthToken=" + User.getInstance().getToken()
				+ "&Duration=0&IncludeAllInfo=true&CourseID=" + module.getId() + "&TitleOnly=true&output=xml";
		DataPuller moduleElementPuller = new DataPuller(new ModuleElementXmlParser());
		moduleElementPuller.execute(url);
		@SuppressWarnings("unchecked")
		Map<ElementType, Element> elements = (Map<ElementType, Element>) moduleElementPuller.get();
		module.getElements().putAll(elements);
		module.getExistence().add(ElementType.ANNOUNCEMENT);
		module.getExistence().addAll(module.getElements().keySet());

	}

	public Module getModule() {
		return module;
	}

	@Override
	public void goToItem(int position) {
		ElementType element = module.getExistence().get(position);
		Intent intent = null;
		switch (element) {
		case FORUM:
			intent = new Intent(this, ForumActivity.class);
			break;
		case WORKBIN:
			intent = new Intent(this, ListWorkbinElementActivity.class);
			break;
		case ANNOUNCEMENT:
			intent = new Intent(this, ListAnnouncementActivity.class);
		default:
		}
		if (intent != null) {
			intent.putExtra(MainActivity.EXTRA_NAME_MODULE_POSITION, modulePosition);
			startActivity(intent);
		}
	}

	public List<ElementType> getDisplayedItemList() {
		return module.getExistence();
	}

	public void checkVoiceCommand() {
		String cmd = getIntent().getStringExtra(VoiceCommandManager.EXTRA_VOICE_COMMAND);
		if (cmd != null) {
			VoiceCommandManager cmdManager = new VoiceCommandManager();
			cmdManager.execute(cmd, this);
		}
	}
}
